package com.source.lightfowl.barcodeMachineRepair.controller;

import com.source.lightfowl.VO.MachineRepairVO;
import com.source.lightfowl.barcodeMachineRepair.pojo.MachineRepair;
import com.source.lightfowl.barcodeMachineRepair.service.MachineRepairService;
import com.source.lightfowl.basedata.entity.BarcodeMachine;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.List;

/**
 * 控制器层
 * @author Administrator
 *
 */
@Api(tags = "脚标机保修管理")
@RestController
@CrossOrigin
@RequestMapping("/machineRepair")
public class MachineRepairController {

	@Autowired
	private MachineRepairService machineRepairService;


	/**
	 * 查询全部数据
	 * @return
	 */
	@ApiOperation(value = "查询全部数据")
	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	public MyResult findAll(){
		List<MachineRepairVO> all = machineRepairService.findAll();
		return MyResult.ok(all);
	}

	@ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）")
	@GetMapping("/findAllPage")
	public MyResult findAllPage(int page,int pageNum){

		List<MachineRepairVO> all = machineRepairService.findAllPage(page,pageNum);
		if(all!=null)
			return MyResult.build(0,"成功",all);
		else
			return MyResult.build(1,"结果为空");
	}
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@ApiOperation(value = "根据ID查询")
	@GetMapping(value="/{id}")
	public MyResult findById(@PathVariable BigInteger id){
		return MyResult.ok(machineRepairService.findById(id));
	}


	/**
	 * 增加
	 * @param machineRepair
	 */
	@ApiOperation(value = "增加")
	@PostMapping
	@LogAnnotation(operEvent = "操作类型：添加",operType = 1)
	public MyResult add(/*@RequestBody*/ MachineRepair machineRepair){
		machineRepairService.add(machineRepair);
		return MyResult.ok();
	}

	/**
	 * 修改
	 * @param machineRepair
	 */
	@ApiOperation("修改")
	@PutMapping(value="/{id}")
	@LogAnnotation(operEvent = "操作类型：修改",operType = 2)
	public MyResult update(/*@RequestBody*/ MachineRepair machineRepair, @PathVariable BigInteger id ){
		machineRepair.setId(id);
		machineRepairService.update(machineRepair);
		return MyResult.ok();
	}

	/**
	 * 删除
	 * @param id
	 */
	@ApiOperation("删除")
	@DeleteMapping(value="/{id}")
	@LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
	public MyResult delete(@PathVariable BigInteger id ){
		machineRepairService.deleteById(id);
		return MyResult.ok();
	}

}
